<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        table {
            margin: 100px auto;
            width: 400px;
            text-align: center;
        }
        thead {
            background-color: skyblue;
        }
      
    </style>
</head>
<body>
   <div class="wrap">
    <table cellspacing='0'>
        <thead>
            <tr>
                <th>
                    <!-- 全选按钮 -->
                   <input type= "checkbox" id='j_cbAll'>全选
                </th>
                <th>商品</th>
                <th>价格</th>
            </tr>
        </thead>
        <tbody id="j_tb">
           <tr>
               <td>
                   <input type="checkbox" />
               </td>
               <td>iphone11</td>
               <td>8000</td>
           </tr>
           <tr>
            <td>
                <input type="checkbox" />
            </td>
            <td>iphone11</td>
            <td>8000</td>
        </tr>
        <tr>
            <td>
                <input type="checkbox" />
            </td>
            <td>iphone11</td>
            <td>8000</td>
        </tr>
        <tr>
            <td>
                <input type="checkbox" />
            </td>
            <td>iphone11</td>
            <td>8000</td>
        </tr>
        </tbody>
    </table>
   </div>
   <script>
       var j_cbAll = document.getElementById('j_cbAll');
       var j_tbs = document.getElementById('j_tb').getElementsByTagName('input');
    //1.全选和取消全选
       j_cbAll.onclick = function() {
        console.log(this.checked);//true全选选中，flase没选中
        for(var i = 0;i<j_tbs.length;i++){
            j_tbs[i].checked = this.checked;
        }
       }
       //2.下面复选框选中部分

       for(var i = 0;i<j_tbs.length;i++){
           j_tbs[i].onclick = function(){
               //flag控制全选是否被选中
                var flag = true;
                //每次点击下面的复选框都要循环检查4个小按钮是否被选中
                for(var i = 0;i<j_tbs.length;i++){
                        if(!j_tbs[i].checked){
                            //只要有一个没有被选中，全选按钮就不会被选中
                        flag = flase;
                        break;
                    }
            }
           j_cbAll.checked = flag;
         }
         
       }

   </script>
</body>
</html>